import { AsyncShowToast } from "../../utils/common";
import moment from 'moment';
import { ApiDivince } from "../../api/api.day";
import { ApiDayResInterface } from "../../interface/api.day.interface";
import { ResInterface } from "../../interface/request.interface";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 签筒动画
    isDivince: false, // 是否正在抽签
    divinceAnimationName: 'divince-animation-base', // 签筒动画
    // 结果
    resultAnimationName: '', // 结果动画名称
    result: {
      base: '', // 基础描述
      time: '', // 创建时间
      list: [], // 详细信息
    } as ApiDayResInterface,
  },

  // 生命周期函数--监听页面加载
  onLoad() {},

  // 生命周期函数--监听页面初次渲染完成
  onReady() {},

  // 生命周期函数--监听页面显示
  onShow() {},

  // 生命周期函数--监听页面隐藏
  onHide() {},

  // 生命周期函数--监听页面卸载
  onUnload() {},

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh() {},

  // 页面上拉触底事件的处理函数
  onReachBottom() {},

  // 用户点击右上角分享
  onShareAppMessage() {},
  // 抽签接口
  async divinceApi(): Promise<ApiDayResInterface> {
    try {
      let _result: ApiDayResInterface = {
        base: '',
        time: '',
        list: [],
      }
      const _ApiDivince: ResInterface = await ApiDivince();
      const { code, data } = _ApiDivince;
      if (code === 200) {
        const _time: string = moment.unix(data['divince_time']).format('YYYY-MM-DD HH:mm:ss');
        _result.base = data['divince_base'];
        _result.time = _time;
        if (data['divince_untie']) {
          let _untie: Object = JSON.parse(data['divince_untie']);
          let _array: Object[] = [];
          const _keyArray: string[] = Object.keys(_untie)
          const _valueArray: any[] = Object.values(_untie)
          if (_keyArray.length) {
            _keyArray.map((item, index) => {
              const _value: any = _valueArray[index];
              const _object: Object = {
                key: item,
                value: _value
              };
              _array.push(_object);
            });
            _result.list = _array;
          }
        }
      }
      console.log('_result', _result)
      return _result;
    } catch (error) {
      console.log('error', error)
      throw error;
    }
  },
  // 点击抽签
  async onDivince() {
    try {
      let _isDivince = this.data.isDivince;
      if (_isDivince) {
        return;
      } else {
        this.setData({
          isDivince: true,
          divinceAnimationName: 'divince-animation',
        });
        const _divinceApi = await this.divinceApi();
        this.setData({
          result: _divinceApi
        });
        // 动画持续时间
        const divinceAnimateTimerFunction = () => {
          return new Promise((resolve) => {
            setTimeout(() => {
              resolve(true);
            }, 2000);
          });
        }
        await divinceAnimateTimerFunction();
        // 弹窗动画
        this.onResultAnimation();
        // 弹窗动画持续时间
        const resultAnimateTimerFunction = () => {
          return new Promise((resolve) => {
            setTimeout(() => {
              resolve(true);
            }, 1000);
          });
        }
        await resultAnimateTimerFunction();
        this.setData({
          isDivince: false,
          divinceAnimationName: 'divince-animation-base',
        });
      }
    } catch (error) {
      console.log('点击抽签', error)
      await AsyncShowToast(error);
    }
  },
  // 结果动画事件
  onResultAnimation() {
    const _resultAnimationName = this.data.resultAnimationName === 'result-animation-show' ?
      'result-animation-hide' :
      'result-animation-show';
    this.setData({
      resultAnimationName: _resultAnimationName,
    });
  },
})